package com.sykj.why.util;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Eleven
 * @time 2019/04/30 14:10
 * @desc 自动生成客流设备uuid和dataguid数据ID
 */
@Slf4j
public class CustDeviceTools {

    //自动生成客流设备UUID
    public static String getCustDeviceUuid(String oraName,String name){
        String uuid = "";
        //汉字转首字母
        String oraNameChar = converterToFirstSpell(oraName);
        String nameChar = converterToFirstSpell(name);
        //生成三位随机数
        int randomNum = (int)(Math.random()*900)+100;
        uuid = oraNameChar + nameChar + Integer.toString(randomNum);
        return uuid;
    }

    //自动生成客流设备数据ID
    public static String getCustDeviceDataGuid(){
        String dataguid = "";
        dataguid = UUID.randomUUID().toString();
        /*System.out.println("********************自动生成的客流设备数据ID为："+dataguid);
        log.info("********************自动生成的客流设备数据ID为："+dataguid);*/
        return dataguid;
    }

        /**
           * 汉字转换位汉语拼音首字母，英文字符不变
           *
           * @param chines
           *            汉字
           * @return 拼音
           */
      public static String converterToFirstSpell(String chines) {
         chines = cleanChar(chines);
         String pinyinName = "";
         char[] nameChar = chines.toCharArray();
         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
         defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
         for (int i = 0; i < nameChar.length; i++) {
                 if (nameChar[i] > 128) {
                         try {
                                 pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0);
                             } catch (BadHanyuPinyinOutputFormatCombination e) {
                                 e.printStackTrace();
                             }
                     } else {
                         pinyinName += nameChar[i];
                     }
             }
         return pinyinName;
     }

     /**
       * 清理特殊字符以便得到
       * @param chines
       * @return
       */
     public static String cleanChar(String chines) {
         chines = chines.replaceAll("[\\p{Punct}\\p{Space}]+", ""); // 正则去掉所有字符操作
         // 正则表达式去掉所有中文的特殊符号
         String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}<>《》【】‘；：”“’。，、？]";
         Pattern pattern = Pattern.compile(regEx);
         Matcher matcher = pattern.matcher(chines);
         chines = matcher.replaceAll("").trim();
         return chines;
     }

    /*public static void main(String[] args){
        System.out.println("uuid:"+getCustDeviceUuid("津南文化馆","客流设备G"));
        System.out.println("dataguid:"+getCustDeviceDataGuid());
    }*/

}
